<template>
  <RkContainer :isFooter="false"
    :isAside=false>
    <template #header_left>
      <RkButtonGroup :options="json_review_button"
        v-model="queryParams.status"
        @click="handleSearch" />
    </template>
    <template #header_right>
      <el-button v-if="queryParams.status==0"
        @click="handleOperation('add')">新增任务</el-button>
      <el-button v-if="queryParams.status==0"
        @click="handleOperation('start','开始任务')">开始任务</el-button>
      <el-button v-if="queryParams.status==0"
        @click="handleOperation('edit','修改')">修改任务</el-button>
      <el-button v-if="queryParams.status==0"
        type="danger"
        @click="handleOperation('delete', '删除')">删除</el-button>
      <el-button v-if="queryParams.status==1"
        type="danger"
        @click="handleOperation('end','停止任务')">停止任务</el-button>
    </template>
    <template #main>
      <RkCenter>
        <template #content>
          <rk-grid ref="taskMangementRef"
            title="评审任务管理"
            dataUrl="/reviewtask/page"
            boxConfig
            :params="queryParams"
            :columns="getTableColumns()">
            <template #toolbar_title_buttons>
              <el-input v-model="queryParams.name"
                placeholder="请输入任务名称"
                class="w-200px"
                clearable
                @input="handleSearch">
              </el-input>
            </template>
            <template #taskName="{ row,column }">
              <a href="javascript:;"
                class="yhs-pro-name"
                @click="handleOperation('info','first',row)">{{ row[column.field] }}</a>
            </template>
            <template #proNum="{ row }">
              <el-button type="primary"
                link
                @click="handleOperation('info','second',row)">{{row.proNum}}</el-button>
            </template>
            <template #expertNum="{ row }">
              <el-button type="primary"
                link
                @click="handleOperation('info','third',row)">{{row.expertNum}}</el-button>
            </template>
            <template #opreation="{ row }">
              <el-button type="primary"
                size="small"
                @click="handleOperation('manual','',row)">手动分配</el-button>
              <el-button type="primary"
                size="small"
                @click="handleOperation('automatic','',row)">规则自动分配</el-button>
            </template>
          </rk-grid>
        </template>
      </RkCenter>
    </template>
    <task-management-edite-drawer ref="taskManagementEditeDrawerRef"
      @save="handleSearch"></task-management-edite-drawer>
    <project-expert-dialog ref="projectExpertRef"></project-expert-dialog>
    <task-management-detail-dialog ref="taskManagementDetilDialogRef"></task-management-detail-dialog>
    <project-manual-expert-dialog ref="projectManualExpertDialogRef"
      @refresh="handleSearch"></project-manual-expert-dialog>
    <project-automatic-expert-dialog ref="projectAutomaticExpertDialogRef"
      @refresh="handleSearch"></project-automatic-expert-dialog>
  </RkContainer>
</template>
  <script setup>

import { json_review_button } from '@/common/button-data.js'
import { EXPERT_INPUT_STATUS } from '@/enums/enums-expert.js'
import { RkConfirmBox } from '@/components/message-box/index.js'
import TaskManagementEditeDrawer from './drawer/TaskManagementEditeDrawer.vue'
import ProjectExpertDialog from './dialogs/ProjectExpertDialog.vue'
import TaskManagementDetailDialog from './dialogs/TaskManagementDetailDialog.vue'
import ProjectManualExpertDialog from './dialogs/ProjectManualExpertDialog.vue'
import ProjectAutomaticExpertDialog from './dialogs/ProjectAutomaticExpertDialog.vue'
import { ref, provide } from 'vue'
import { useRoute } from 'vue-router'
import { ElLoading } from "element-plus"
import { useExpertsApi } from '@/api/index'
const { reviewtaskDeleteData, reviewtaskStartTaskData, reviewtaskStopTaskData } = useExpertsApi();

const { appContext } = getCurrentInstance();
const globalProxy = appContext.config.globalProperties;
const taskMangementRef = ref(null)
const taskManagementEditeDrawerRef = ref(null)
const projectExpertRef = ref(null)
const taskManagementDetilDialogRef = ref(null)
const projectManualExpertDialogRef = ref(null)
const projectAutomaticExpertDialogRef = ref(null)
let route = useRoute()
// 查询参数
let queryParams = reactive({
  name: undefined,
  agId: undefined,
  status: parseInt(history.state.wfStatus) || 0,
  nodeId: route.query.nodeId,
})
provide("nodeId", queryParams.nodeId);
const getTableColumns = () => {
  const tableColumns = [
    {
      field: 'name', title: '任务名称', width: 250, align: 'left', slots: { default: 'taskName' }
    },
    {
      field: 'type', title: '类型', width: 120, align: 'center', slots: {
        default: ({ row }) => {
          return row.type ? EXPERT_INPUT_STATUS.options[row.type].label : ""
        }
      }
    },
    { field: 'createTime', title: '创建日期', width: 160, align: 'center', },
    {
      field: 'startDate', title: '开始日期', width: 150, align: 'center', slots: {
        default: ({ row }) => {
          return row.startDate ? row.startDate : "-"
        }
      }
    },
    {
      field: 'endDate', title: '结束日期', width: 150, align: 'center', slots: {
        default: ({ row }) => {
          return row.endDate ? row.endDate : "-"
        }
      }
    },
    { field: 'proNum', title: '项目总数', width: 120, align: 'center', slots: { default: 'proNum' } },
    { field: 'expertNum', title: '专家总数', width: 120, align: 'center', slots: { default: 'expertNum' } },
    { field: 'remark', title: '备注', width: 120, align: 'left' },
    {
      field: 'opreation', title: '操作', width: 250, align: 'center', fixed: 'right', visible: queryParams.status === 0, slots: { default: 'opreation' }
    },
  ]
  return tableColumns
}
/**查询 */
const handleSearch = () => {
  taskMangementRef.value.searchEvent()
}
//送审、撤销送审、删除
const handleOperation = async (buttonType, textName, row) => {
  if (buttonType === 'add') {
    taskManagementEditeDrawerRef.value.handleOpen()
  } else if (buttonType === 'edit') { //修改
    const seleRows = taskMangementRef.value.getCheckboxRecords()
    if (seleRows.length != 1 || !seleRows) {
      globalProxy.$message({ message: `请选择一条数据进行${textName}！`, type: "warning" });
      return;
    }
    const taskId = seleRows[0].taskId
    taskManagementEditeDrawerRef.value.handleOpen(taskId)
  } else if (buttonType == 'info') {
    const taskId = row.taskId
    let type = textName
    taskManagementDetilDialogRef.value.handleOpen(taskId, type)
  } else if (buttonType == 'manual') { //按照项目手动分配
    const taskId = row.taskId
    const taskName = row.name
    projectManualExpertDialogRef.value.handleOpen(taskId, taskName)
  } else if (buttonType == 'automatic') { //按规则自动分配
    const taskId = row.taskId
    const taskName = row.name
    projectAutomaticExpertDialogRef.value.handleOpen(taskId, taskName)
  } else {
    const seleRows = taskMangementRef.value.getCheckboxRecords()
    if (seleRows.length == 0 || !seleRows) {
      globalProxy.$message({ message: `请至少选择一条数据进行${textName}！`, type: "warning" });
      return;
    } else {
      RkConfirmBox({
        title: '提示',
        content: `您确定要${textName}吗？`,
        onSubmit: async function () {
          //获取选中数据
          const TaskId = []
          seleRows.forEach(element => {
            TaskId.push(element.taskId)
          });
          const taskIds = TaskId.join(',')
          const loading = ElLoading.service({ fullscreen: true })
          if (buttonType == 'delete') {  //删除
            const params = { taskIds, nodeId: queryParams.nodeId }
            const result = await reviewtaskDeleteData(params)
            const { data, code, msg } = result
            if (code === 200) {
              loading.close()
              globalProxy.$message({ message: `${textName}成功！`, type: "success" });
              handleSearch()
            } else {
              loading.close()
              globalProxy.$message({ message: `${msg}`, type: "error" });
            }
          } else if (buttonType == 'start') { //开始
            const params = { taskIds, nodeId: queryParams.nodeId }
            const result = await reviewtaskStartTaskData(params)
            const { data, code, msg } = result
            if (code === 200) {
              loading.close()
              globalProxy.$message({ message: `${textName}成功！`, type: "success" });
              handleSearch()
            } else {
              loading.close()
              globalProxy.$message({ message: `${msg}`, type: "error" });
            }
          } else if (buttonType == 'end') { //结束
            const params = { taskIds, nodeId: queryParams.nodeId }
            const result = await reviewtaskStopTaskData(params)
            const { data, code, msg } = result
            if (code === 200) {
              loading.close()
              globalProxy.$message({ message: `${textName}成功！`, type: "success" });
              handleSearch()
            } else {
              loading.close()
              globalProxy.$message({ message: `${msg}`, type: "error" });
            }
          }
        },
        onCancel: function () {
        }
      })
    }
  }
}
  </script>
  <style lang="scss" scoped>
.w-200px {
	width: 200px;
}
</style>
  